<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('债务管理列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>债务名称：</label>
                                <input type="text" name="name"/>
                            </li>
                            <li>
                                <label>开始日期：</label>
                                <input type="text" class="time-input" placeholder="请选择开始日期" name="startDate"/>
                            </li>
                            <li>
                                <label>结束日期：</label>
                                <input type="text" class="time-input" placeholder="请选择结束日期" name="endDate"/>
                            </li>
                            <li>
                                <label>债权人：</label>
                                <input type="text" name="creditor"/>
                            </li>
                            <li>
                                <label>经办人：</label>
                                <input type="text" name="operator"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:debt:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:debt:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:debt:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:debt:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:debt:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:debt:remove')}]];
        var prefix = ctx + "system/debt";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                stageUrl: prefix + "/stage/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "债务管理",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '',
                    visible: false
                },
                {
                    field: 'name',
                    title: '债务名称'
                },
                {
                    field: 'startDate',
                    title: '开始日期'
                },
                {
                    field: 'endDate',
                    title: '结束日期'
                },
                {
                    field: 'creditor',
                    title: '债权人'
                },
                {
                    field: 'operator',
                    title: '经办人'
                },
                {
                    field: 'expire',
                    title: '到期提醒',
                    formatter: function(value, row, index) {
                        var actions = [];
                        // 获取今天的日期
                        let today = new Date();
                        // 获取目标日期
                        let targetDate = new Date(row.endDate);
                        // 计算距离今天的天数
                        let diffTime = targetDate.getTime() - today.getTime();
                        let diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
                        if(diffDays > 30 || diffDays < 1){
                            // $.table.hideColumn('endDate')
                            // return actions.join('');
                        }else if(diffDays > 15 && diffDays < 30){
                            actions.push('<span class="badge badge-info">' + diffDays +'天'+ '</span>');
                            return actions.join('');
                        }else if(diffDays > 7 && diffDays < 15){
                            actions.push('<span class="badge badge-warning">' + diffDays +'天'+ '</span>');
                            return actions.join('');
                        }else if(diffDays > 0 && diffDays < 7){
                            actions.push('<span class="badge badge-danger">' + diffDays +'天'+ '</span>');
                            return actions.join('');
                        }
                    }
                },
                {
                    field: 'slippage',
                    title: '逾期提醒',
                    formatter: function(value, row, index) {
                        var actions = [];
                        // 获取今天的日期
                        let today = new Date();
                        // 获取目标日期
                        let targetDate = new Date(row.endDate);
                        // 计算距离今天的天数
                        let diffTime =  today.getTime() - targetDate.getTime();
                        let diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
                        if(diffDays > 30 || diffDays < 1){
                            // $.table.hideColumn('endDate')
                            // return actions.join('');
                        }else if(diffDays > 15 && diffDays < 30){
                            actions.push('<span class="badge badge-info">' + diffDays +'天'+ '</span>');
                            return actions.join('');
                        }else if(diffDays > 7 && diffDays < 15){
                            actions.push('<span class="badge badge-warning">' + diffDays +'天'+ '</span>');
                            return actions.join('');
                        }else if(diffDays > 0 && diffDays < 7){
                            actions.push('<span class="badge badge-danger">' + diffDays +'天'+ '</span>');
                            return actions.join('');
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="stageFull(\'' + row.id + '\')"><i class="fa fa-edit"></i>分期管理</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        // 分期管理 全屏
        function stageFull(id) {
            table.set();
            var url = "/404.html";
            if ($.common.isNotEmpty(id)) {
                url = table.options.stageUrl.replace("{id}", id);
            } else {
                if(table.options.type == table_type.bootstrapTreeTable) {
                    var row = $("#" + table.options.id).bootstrapTreeTable('getSelections')[0];
                    if ($.common.isEmpty(row)) {
                        $.modal.alertWarning("请至少选择一条记录");
                        return;
                    }
                    url = table.options.stageUrl.replace("{id}", row[table.options.uniqueId]);
                } else {
                    var row = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
                    url = table.options.stageUrl.replace("{id}", row);
                }
            }
            $.modal.openFull("分期管理", url);
        }
    </script>
</body>
</html>